<template>
  <div class="login-container">
    <div class="login-card">
      <h2 class="login-title">AI任务拆分与管理系统</h2>
      <el-form :model="loginForm" ref="loginFormRef" :rules="loginRules" class="login-form">
        <el-form-item prop="tenant_code">
          <el-select v-model="loginForm.tenant_code" placeholder="请选择租户" class="tenant-select">
            <el-option
              v-for="item in tenants"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名">
            <template #prefix>
              <span class="el-icon-user"></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="密码" type="password">
            <template #prefix>
              <span class="el-icon-lock"></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
        <div class="login-options">
          <router-link to="/register" class="register-link">创建新账号</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const store = useStore()
    const loginFormRef = ref(null)
    const loading = ref(false)
    const tenants = ref([])
    
    const loginForm = reactive({
      username: '',
      password: '',
      tenant_code: 'default'
    })
    
    const loginRules = {
      tenant_code: [
        { required: true, message: '请选择租户', trigger: 'change' }
      ],
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    // 获取租户列表
    const fetchTenants = async () => {
      try {
        const tenantsData = await store.dispatch('fetchAvailableTenants');
        if (tenantsData && tenantsData.length > 0) {
          tenants.value = tenantsData;
          
          // 如果有租户，默认选择第一个
          loginForm.tenant_code = tenants.value[0].code;
        } else {
          // 如果没有获取到租户，使用默认租户
          tenants.value = [{ code: 'default', name: '默认租户' }];
          loginForm.tenant_code = 'default';
        }
      } catch (error) {
        console.error('获取租户列表失败', error);
        // 如果获取租户失败，添加默认租户
        tenants.value = [{ code: 'default', name: '默认租户' }];
        loginForm.tenant_code = 'default';
      }
    }
    
    onMounted(() => {
      fetchTenants()
    })
    
    const handleLogin = () => {
      loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            const result = await store.dispatch('login', loginForm)
            if (result) {
              ElMessage.success('登录成功')
              router.push('/')
            }
          } catch (error) {
            ElMessage.error(error.message || '登录失败，请重试')
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    const goToRegister = () => {
      router.push('/register')
    }
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      tenants,
      handleLogin,
      goToRegister
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f2f5;
  background-image: linear-gradient(to right, #3a7bd5, #00d2ff);
}

.login-card {
  width: 400px;
  padding: 40px;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-weight: bold;
}

.login-form {
  width: 100%;
  max-width: 400px;
}

.login-button {
  width: 100%;
}

.tenant-select {
  width: 100%;
}

.login-options {
  margin-top: 15px;
  text-align: center;
}

.register-link {
  font-size: 14px;
  color: #409eff;
  text-decoration: none;
}

.register-link:hover {
  text-decoration: underline;
}
</style> 